from launch import LaunchDescription, actions, substitutions
from launch_ros.actions import Node
import launch_ros
from ament_index_python import get_package_share_directory
import os

import launch_ros.parameter_descriptions

def generate_launch_description():
    
    urdf_package_path = get_package_share_directory('fishrobot_description')
    default_urdf_path = os.path.join(urdf_package_path, 'urdf', 'first_robot.urdf')
    # 声明一个urdf目录参数
    action_declare_arg_mode_path = actions.DeclareLaunchArgument(
        name='model', default_value=str(default_urdf_path),description="加载模型文件路径"
    )
    
    
    # 从声明的urdf参数里获取参数值
    model_path = substitutions.LaunchConfiguration('model')
    # 获取urdf文件的内容，获取参数值并传入 robot_state_publisher
    urdf_content = substitutions.Command(['xacro ', model_path])
    robot_description_value = launch_ros.parameter_descriptions.ParameterValue(urdf_content, value_type=str)
    
    
    action_robot_state_publisher = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        parameters=[{'robot_description': robot_description_value}]
    )
    
    joint_state_publisher = Node(
        package='joint_state_publisher',
        executable='joint_state_publisher',
    )
    
    action_rviz_node = Node(
        package='rviz2',
        executable='rviz2',
    )

    return LaunchDescription([
        action_declare_arg_mode_path,
        action_robot_state_publisher,
        joint_state_publisher,
        action_rviz_node,
    ])